﻿using System;
using System.Collections.Generic;
using System.Text;
using PlugNT.Custom;

namespace PlugNT.Cms.Page
{
    /// <summary>
    /// 自动生成
    /// </summary>
    public class AutoUpdate
    {
        //定时日期
        private static string datespan = "";
        /// <summary>
        /// 开始
        /// </summary>
        public static void Start()
        {
            if (WebConfig.IsAutoUpdate)
            {
                Exec();
            }
        }
        /// <summary>
        /// 终止
        /// </summary>
        public static void Abort()
        {
            if (timerClock != null)
            {
                timerClock.Enabled = false;
            }
        }
        /// <summary>
        /// 销毁
        /// </summary>
        private static void Dispose()
        {
            if (timerClock != null)
            {
                timerClock.Enabled = false;
                timerClock.Dispose();
                timerClock = null;
            }
        }


        #region Timer(时间控件)
        //创建一个计时器，单位：毫秒
        private static System.Timers.Timer timerClock = null;
        private static void OnTimer(Object source, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                DateTime dt = DateTime.Now;
                string dayFlag = datespan.Substring(0, 1);
                string autoDayString = GetWeekToString(dayFlag);
                string currDayString = GetWeekToString(dt.DayOfWeek);
                string autoTimeString = datespan.Substring(2);
                string currTimeString = dt.Hour.ToString() + "-" + dt.Minute.ToString() + "-" + dt.Second.ToString();
                if (dayFlag == "0" || autoDayString == currDayString)
                {
                    if (autoTimeString == currTimeString)
                    {
                        if (WebConfig.IsAutoUpdate)
                        {
                            PageBuilder.UpdatePages();
                        }
                        else
                        {
                            Abort();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                timerClock.Enabled = false;
            }
        }

        /// <summary>
        /// 执行
        /// </summary>
        private static void Exec()
        {
            datespan = WebConfig.AutoDateSpan;
            if (timerClock == null)
            {
                timerClock = new System.Timers.Timer();
                timerClock.Elapsed += new System.Timers.ElapsedEventHandler(OnTimer);
            }
            timerClock.Interval = 1000;
            timerClock.Enabled = true;

        }

        #endregion


        #region 当前窗体通用函数
        public static string GetWeekToString(string flag)
        {
            string weekstr = "";
            switch (flag)
            {
                case "1":
                    weekstr = "星期一";
                    break;
                case "2":
                    weekstr = "星期二";
                    break;
                case "3":
                    weekstr = "星期三";
                    break;
                case "4":
                    weekstr = "星期四";
                    break;
                case "5":
                    weekstr = "星期五";
                    break;
                case "6":
                    weekstr = "星期六";
                    break;
                case "7":
                    weekstr = "星期日";
                    break;
                default:
                    weekstr = "每天";
                    break;
            }
            return weekstr;
        }
        public static string GetWeekToString(System.DayOfWeek week)
        {
            string weekstr = "";
            switch (week)
            {
                case System.DayOfWeek.Monday:
                    weekstr = "星期一";
                    break;
                case System.DayOfWeek.Tuesday:
                    weekstr = "星期二";
                    break;
                case System.DayOfWeek.Wednesday:
                    weekstr = "星期三";
                    break;
                case System.DayOfWeek.Thursday:
                    weekstr = "星期四";
                    break;
                case System.DayOfWeek.Friday:
                    weekstr = "星期五";
                    break;
                case System.DayOfWeek.Saturday:
                    weekstr = "星期六";
                    break;
                case System.DayOfWeek.Sunday:
                    weekstr = "星期日";
                    break;
                default:
                    weekstr = "每天";
                    break;
            }
            return weekstr;
        }
        public static string GetWeekToInt(System.DayOfWeek week)
        {
            string weekstr = "";
            switch (week)
            {
                case System.DayOfWeek.Monday:
                    weekstr = "1";
                    break;
                case System.DayOfWeek.Tuesday:
                    weekstr = "2";
                    break;
                case System.DayOfWeek.Wednesday:
                    weekstr = "3";
                    break;
                case System.DayOfWeek.Thursday:
                    weekstr = "4";
                    break;
                case System.DayOfWeek.Friday:
                    weekstr = "5";
                    break;
                case System.DayOfWeek.Saturday:
                    weekstr = "6";
                    break;
                case System.DayOfWeek.Sunday:
                    weekstr = "7";
                    break;
                default:
                    weekstr = "0";
                    break;
            }
            return weekstr;
        }
        #endregion

    }
}
